﻿Public Class frmLopHoc

    Sub LoadDanhSach()
        Dim _lh As DataTable
        _lh = LopHocDAO.SelectAll()
        dgvTTLopHoc.DataSource = _lh
    End Sub

    Sub Display()
        If (dgvTTLopHoc.SelectedRows.Count > 0) Then
            txtMALOP.Text = dgvTTLopHoc.SelectedRows(0).Cells(0).Value.ToString()
            txtTENLOP.Text = dgvTTLopHoc.SelectedRows(0).Cells(1).Value.ToString()
            dtpNAMTUYENSINH.Value = dgvTTLopHoc.SelectedRows(0).Cells(2).Value
        End If
    End Sub

    Private Sub frmLopHoc_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dgvTTLopHoc.Rows.Clear()
        LoadDanhSach()
    End Sub

    Private Sub dgvTTLopHoc_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvTTLopHoc.CellContentClick
        Display()
    End Sub

    Private Sub dgvTTLopHoc_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvTTLopHoc.SelectionChanged
        Display()
    End Sub

    Private Sub frmLopHoc_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        If (dgvTTLopHoc.SelectedRows.Count > 0) Then
            If MessageBox.Show("Bạn có muốn lưu lại những thay đổi", "Thông Báo", MessageBoxButtons.YesNo) = DialogResult.Yes Then
                Dim _lh As LopHocDTO = New LopHocDTO()
                _lh.MaLop = txtMALOP.Text
                _lh.TenLop = txtTENLOP.Text
                _lh.NamTuyenSinh = dtpNAMTUYENSINH.Text
                Try
                    LopHocDAO.Update(_lh)
                Catch ex As Exception
                    MessageBox.Show(ex.ToString())
                End Try
                LoadDanhSach()
            End If
        End If
    End Sub

    Private Sub btnTHEM_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTHEM.Click
        Dim _lh As LopHocDTO = New LopHocDTO()
        _lh.TenLop = txtTENLOP.Text
        _lh.NamTuyenSinh = dtpNAMTUYENSINH.Text
        Try
            LopHocDAO.Insert(_lh)
            MessageBox.Show("Thêm lớp học thành công.")
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try
        LoadDanhSach()
    End Sub

    Private Sub btnSUA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSUA.Click
        If (dgvTTLopHoc.SelectedRows.Count > 0) Then
            Dim _lh As LopHocDTO = New LopHocDTO()
            _lh.MaLop = txtMALOP.Text
            _lh.TenLop = txtTENLOP.Text
            _lh.NamTuyenSinh = dtpNAMTUYENSINH.Text
            Try
                LopHocDAO.Update(_lh)
                MessageBox.Show("Cập nhật lớp học thành công.")
            Catch ex As Exception
                MessageBox.Show(ex.ToString())
            End Try
            LoadDanhSach()
        End If
    End Sub

    Private Sub btnXOA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXOA.Click
        Dim _lh As LopHocDTO = New LopHocDTO()
        _lh.MaLop = txtMALOP.Text
        Try
            LopHocDAO.Delete(_lh)
            MessageBox.Show("Xóa lớp học thành công.")
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        End Try
        LoadDanhSach()
    End Sub

    Private Sub btnTHOAT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTHOAT.Click
        Me.Close()
    End Sub
End Class